package it.unibz.easytravel.server.util;

import java.io.*;
import java.util.zip.*;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;

//This class works only with 1 FILE
public class ZipTools {
	public static InputStream unzip(InputStream in) throws IOException {
		ZipInputStream is = new ZipInputStream(in);
		is.getNextEntry();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len;
		while ((len = is.read(buffer)) >= 0) {
			out.write(buffer, 0, len);
		}
		is.close();
		out.close();
		InputStream ins = new ByteInputStream(out.toByteArray(), out.size());
		return ins;
	}

	public static InputStream zip(InputStream in, String fileName)
			throws IOException {
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		ZipOutputStream out = new ZipOutputStream(bout);
		byte[] buffer = new byte[1024];
		int len;
		out.putNextEntry(new ZipEntry(fileName));
		while ((len = in.read(buffer)) >= 0) {
			out.write(buffer, 0, len);
		}
		in.close();
		out.closeEntry();
		out.close();
		InputStream is = new ByteInputStream(bout.toByteArray(), bout.size());
		return is;
	}

	public static void main(String[] args) {
		try {
			InputStream is = new FileInputStream(new File("test.txt"));
			ZipTools.zip(is, "test.zip");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}